package com.nongtai.utils;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author yza
 * @version 1.0
 * @date 2024/7/11 17:22
 */

public class MD5Util {

    public static String encryptToMD5(String input){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashInBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

            // 将字节数组转换成十六进制字符串
            BigInteger number = new BigInteger(1, hashInBytes);
            StringBuilder hexString = new StringBuilder(number.toString(16));

            // 确保字符串长度为32位
            while (hexString.length() < 32) {
                hexString.insert(0, '0');
            }

            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
